package com.example.Arithmetic.Lettcode;

import java.util.Arrays;

/**
 * 日期：2024/1/3
 * 时间：9:01
 * 描述：最少有k个重复字符的最长字串
 */
public class E01Lettcode395 {
    public static int longestSubstring(String s, int k) {
        if (s.length() < k) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int[] ints = new int[26];
        for (char c : chars) {
            ints[c - 'a']++;
        }
        System.out.println(Arrays.toString(ints));
        for (int i = 0; i < s.length(); i++) {
            char c = chars[i];
            int anInt = ints[c - 'a'];
            if (0 < anInt && anInt < k) {
                int j = i + 1;
                while (j < s.length() && ints[chars[j] - 'a'] < k) {
                    j++;
                }
                return Integer.max(longestSubstring(s.substring(0, i), k),
                        longestSubstring(s.substring(j), k));
            }
        }
        return s.length();
    }

    public static void main(String[] args) {
        System.out.println(longestSubstring("dddxaabaaabaacciiiiefbff", 3));
    }
}

